package com.gxp.controller;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gxp.pojo.User;
import com.gxp.service.SiteSettingService;
import com.gxp.util.common.GXPConstant;
import com.gxp.util.common.GXPUtil;
import com.gxp.util.security.Des;
import com.gxp.util.security.RSAUtil;

@Controller
public class LoginController {
	
	Logger logger = Logger.getLogger(LoginController.class);
	
	@Autowired
	private SiteSettingService siteSettingService;

	@RequestMapping(value="/admin/sitesetting",method=RequestMethod.GET)
	public ModelAndView handleLogin(User user) throws Exception {
		logger.debug("SiteInfoController editSiteINfo is invoked.........");
		
		Map<String, String> settings = siteSettingService.getSiteSetting();
		logger.debug("settings size: " +  settings.size());
		String pwd = settings.get(GXPConstant.SITE_SETTING_KEY_EMAIL_PASSWORD);
		if (StringUtils.hasText(pwd)) {
			settings.put(GXPConstant.SITE_SETTING_KEY_EMAIL_PASSWORD, Des.decrypt(pwd));
		}
		ModelAndView mav = new ModelAndView();
		mav.addObject("settings", settings);
		mav.setViewName("admin/sitesetting/siteSettingBaseInfo");
		return mav;		
	}
	
	@RequestMapping(value="/admin/user/main",method=RequestMethod.GET)
	public ModelAndView handleLogin(HttpServletRequest request) {
		
		
		ModelAndView mav = new ModelAndView();
//		mav.addObject("siteSetting", siteSetting);
		mav.setViewName("admin/user/main");
		return mav;		
	}
	
	@RequestMapping(value="/admin/index",method=RequestMethod.GET)
	public ModelAndView handleLoginPage(HttpServletRequest request) {
		logger.debug("handleLoginPage................." + request.getLocale());		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("admin/index");
		return mav;		
	}

	@RequestMapping(value="/admin",method=RequestMethod.POST)
	public ModelAndView handleIndex() throws Exception {
		logger.debug("handleIndex");		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("admin/index");
		return mav;		
	}

	@RequestMapping(value="/rsa/getkeystr",method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public String getPublicKeyStr() {
		logger.debug("get rsa public key...");
		String pkeyExp = RSAUtil.getInstance().getPublicKeyExp();
		String pkeyMod = RSAUtil.getInstance().getPublicMod();
		Map<String, String> keyvalue = new HashMap<String, String>();
		keyvalue.put("pkeyExp", pkeyExp);
		keyvalue.put("pkeyMod", pkeyMod);
		logger.debug("pkeyExp: " + pkeyExp);
		logger.debug("pkeyMod: " + pkeyMod);
		return GXPUtil.getJsonString(keyvalue);
	}
}
